﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FieldDevice;
using FieldDevice.Emulation;
using FieldDevice.ModBus;

namespace FieldDevice.UI
{
    public  partial class DeviceFormBase : Form
    {
        public int ListOrderCount=1;
        public DeviceFormBase(Device device)
        {
            if (device == null)
                throw new ArgumentNullException("device parameter should not be null!");
            this.Device1 = device;
            InitializeComponent();
        }
        public DeviceFormBase()
        {
            InitializeComponent();
        }

       public virtual void UpdateReads()
       {
       }

        protected delegate void VoidDelegate();
        protected VoidDelegate UpdateReadsDelegate;
        protected Device.ReadChangeEventHander ReadChangeEventHander;
        public Device Device1;

        private void DeviceFormBase_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(this.Device1!=null)
                this.Device1.ReadChangeEvent -= this.ReadChangeEventHander;
        }

        public ListViewItem ListDetail(int order, string var, string varname, string value)
        {
            ListOrderCount++;
            ListViewItem list = new ListViewItem(order.ToString());
            list.SubItems.Add(var);
            list.SubItems.Add(varname);
            list.SubItems.Add(value.ToString());
            return list;
        }

        public void ConfigDevice()
        {
            DeviceConfigDialog dlg = new DeviceConfigDialog(this.Device1);
            if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
            }
        }

        private void DeviceFormBase_Load(object sender, EventArgs e)
        {
            if (this.Device1 == null)
            {
                this.Text += "No device is set!";
            }
            else
            {
                this.Text = this.Device1.DeviceName + " " + this.Device1.Description +
                    "(" + this.Device1.GetType().Name + " 地址:" + this.Device1.NCPAddress.ToString("X") + ")";
            }
        }
    }


}
